package com.duanjw.springframework.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author duanjw
 */
public class ScopeApplication {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(AppConfig.class);
        SingletonBean sb1 = cxt.getBean(SingletonBean.class);
        SingletonBean sb2 = cxt.getBean(SingletonBean.class);
        PrototypeBean pb1 = cxt.getBean(PrototypeBean.class);
        PrototypeBean pb2 = cxt.getBean(PrototypeBean.class);
        System.out.println(sb1.hashCode());
        System.out.println(sb2.hashCode());
        System.out.println(pb1.hashCode());
        System.out.println(pb2.hashCode());

        si1(cxt);
        si2(cxt);
    }

    public static void si1(AnnotationConfigApplicationContext ctx){
        System.out.println("---------单例中获取一个原型bean1----");
        SingletonWithPrototypeBean1 bean = ctx.getBean(SingletonWithPrototypeBean1.class);
        for (int i = 0; i < 3; i++) {
            bean.show();
        }
    }

    public static void si2(AnnotationConfigApplicationContext ctx){
        System.out.println("---------单例中获取一个原型bean2----");
        SingletonWithPrototypeBean2 bean = ctx.getBean(SingletonWithPrototypeBean2.class);
        for (int i = 0; i < 3; i++) {
            bean.show();
        }
    }
}

